package com.lanren.familybookapp.ui.home.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lanren.common.interfaces.AdapterItemClickView;
import com.lanren.common.utils.StringUtils;
import com.lanren.familybookapp.R;
import com.lanren.familybookapp.ui.home.bean.BookGroupBean;
import com.lanren.hovertitlerecylerview.HoverTitleAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ClassName HomeBookAdapter
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/10/6 20:20
 * @Version 1.0
 */
public class HomeBookAdapter extends HoverTitleAdapter<RecyclerView.ViewHolder> {
    private static final int VIEW_TYPE_ITEM_TIME = 0;
    private static final int VIEW_TYPE_ITEM_CONTENT = 1;

    private List<BookGroupBean> list;
    private AdapterItemClickView adapterItemClickView;

    public HomeBookAdapter(List<BookGroupBean> list) {
        this.list = list;
    }

    @Override
    public boolean isHoverPosition(int position) {
        return getItemViewType(position) == VIEW_TYPE_ITEM_TIME;
    }

    public void setAdapterItemClickView(AdapterItemClickView adapterItemClickView) {
        this.adapterItemClickView = adapterItemClickView;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == VIEW_TYPE_ITEM_TIME) {
            return new TitleHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_book_title, viewGroup, false));
        } else {
            return new ContentHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_book_content, viewGroup, false));
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        if (getItemViewType(i) == VIEW_TYPE_ITEM_TIME) {
            TitleHolder titleHolder = (TitleHolder) viewHolder;
            titleHolder.titleTime.setText(list.get(i).getBookTime().substring(5, 10));
            titleHolder.titleInMoney.setText("+" + list.get(i).getPayInMoneyDay());
            titleHolder.titleOutMoney.setText("-" + list.get(i).getPayOutMoneyDay());
        } else {
            ContentHolder contentHolder = (ContentHolder) viewHolder;

            String classifyName = list.get(i).getBooksBeanList().getBookClassify();
            contentHolder.bookClassifyImg.setText(classifyName.substring(0, 1));
            contentHolder.bookClassifyName.setText(classifyName);

            if (list.get(i).getBooksBeanList().getInOut().equals("支出")) {
                contentHolder.bookMoney.setText("-" + list.get(i).getBooksBeanList().getMoney());
            } else {
                contentHolder.bookMoney.setText("+" + list.get(i).getBooksBeanList().getMoney());
            }


            contentHolder.bookGoodsLayout.setVisibility(View.VISIBLE);
            contentHolder.bookGoodsName.setText(list.get(i).getBooksBeanList().getGoods());

            contentHolder.bookMemberLayout.setVisibility(View.VISIBLE);
            contentHolder.bookMember.setText(list.get(i).getBooksBeanList().getBookMember());

            if (StringUtils.isNotBlank(list.get(i).getBooksBeanList().getPs())) {
                contentHolder.bookPsLayout.setVisibility(View.VISIBLE);
                contentHolder.bookPs.setText(list.get(i).getBooksBeanList().getPs());
            } else {
                contentHolder.bookPsLayout.setVisibility(View.GONE);
            }


            contentHolder.bookContentLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (adapterItemClickView != null) {
                        adapterItemClickView.onItemClick(i);
                    }
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (list.get(position).isGroup()) {
            return VIEW_TYPE_ITEM_TIME;
        } else {
            return VIEW_TYPE_ITEM_CONTENT;
        }
    }

    static class ContentHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.book_classify_img)
        TextView bookClassifyImg;
        @BindView(R.id.book_classify_name)
        TextView bookClassifyName;
        @BindView(R.id.book_money)
        TextView bookMoney;
        @BindView(R.id.book_goods_name)
        TextView bookGoodsName;
        @BindView(R.id.book_goods_layout)
        LinearLayout bookGoodsLayout;
        @BindView(R.id.book_member)
        TextView bookMember;
        @BindView(R.id.book_member_layout)
        LinearLayout bookMemberLayout;
        @BindView(R.id.book_ps)
        TextView bookPs;
        @BindView(R.id.book_ps_layout)
        LinearLayout bookPsLayout;
        @BindView(R.id.book_content_layout)
        RelativeLayout bookContentLayout;

        public ContentHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class TitleHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.title_time)
        TextView titleTime;
        @BindView(R.id.title_in_money)
        TextView titleInMoney;
        @BindView(R.id.title_out_money)
        TextView titleOutMoney;

        public TitleHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
